/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.tesoreria.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.tesoreria.be.CuentaContable;
import org.cip.esymons.tesoreria.be.EnumEstadoCuentaContable;

/**
 *
 * @author Administrador
 */
@Stateless
public class CuentaContableFacade implements CuentaContableFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(CuentaContable cuentaContable) {
        em.persist(cuentaContable);
    }

    public void edit(CuentaContable cuentaContable) {
        em.merge(cuentaContable);
    }

    public void remove(CuentaContable cuentaContable) {
        CuentaContable aux = em.merge(cuentaContable);
        em.remove(aux);
    }

    public CuentaContable find(Object id) {
        try {
            return em.find(CuentaContable.class, id);
        } catch (Exception e) {
            return null;
        }
    }

    public List<CuentaContable> findAll() {
        try {
            return em.createQuery("select object(o) from CuentaContable as o").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<CuentaContable> paginadorCuentaContable(int cantidad, int posicion, String codigo, String desc) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select c From CuentaContable c ");
            Integer cont = 0;
            if (codigo != null && !codigo.isEmpty()) {
                sb.append(" Where ");
                sb.append(" c.ccoCodigo =?").append((++cont).toString());
            }
            if (desc != null && !desc.isEmpty()) {
                if (cont.equals(0)) {
                    sb.append(" Where ");
                } else {
                    sb.append(" and ");
                }
                sb.append(" c.ccoDesc like ?").append((++cont).toString());
            }
            q = em.createQuery(sb.toString());
            cont = 0;
            if (codigo != null && !codigo.isEmpty()) {
                q.setParameter(++cont, codigo);
            }
            if (desc != null && !desc.isEmpty()) {
                q.setParameter(++cont, "%" + desc + "%");
            }
            q.setFirstResult(posicion);
            q.setMaxResults(cantidad);
            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }

    public int contarPaginadorCuentaContable(String codigo, String desc) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select count(c) From CuentaContable c ");
            Integer cont = 0;
            if (codigo != null && !codigo.isEmpty()) {
                sb.append(" Where ");
                sb.append(" c.ccoCodigo like ?").append((++cont).toString());
            }
            if (desc != null && !desc.isEmpty()) {
                if (cont.equals(0)) {
                    sb.append(" Where ");
                } else {
                    sb.append(" and ");
                }
                sb.append(" c.ccoDesc like ?").append((++cont).toString());
            }
            q = em.createQuery(sb.toString());
            cont = 0;
            if (codigo != null && !codigo.isEmpty()) {
                q.setParameter(++cont, codigo + "%");
            }
            if (desc != null && !desc.isEmpty()) {
                q.setParameter(++cont, "%" + desc + "%");
            }
            return new Integer(q.getSingleResult().toString());
        } catch (Exception ex) {
            return 0;
        }
    }

    public List<CuentaContable> listaCuentasContablesActivasyDivisionariasPorCodigo(String codigo) {
        try {
            Query q = em.createQuery("Select o From CuentaContable o Where o.ccoCodigo like ?1 and o.ccoEstado=?2 and o.esDivisionaria=true");
            q.setParameter(1, codigo + "%");
            q.setParameter(2, EnumEstadoCuentaContable.Activa);
            return q.getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<CuentaContable> listadoCuentasContablesActivas(String cond) {
        try {
            Query q = em.createQuery("Select o From CuentaContable o Where o.ccoEstado=?1 and o.ccoCodigo like ?2");
            q.setParameter(1, EnumEstadoCuentaContable.Activa);
            q.setParameter(2, cond + "%");
            return q.getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }
}
